package com.kyxx.topics.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.kyxx.topics.R;
import com.kyxx.topics.activity.DetailActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class VipCardFragment extends Fragment {
    private ListView lv;
    private View topView;
    private Banner banner;
    private RecyclerView rv;
    private Context context;
    private PtrClassicFrameLayout ptrv;
    private RelativeLayout layoutProgress;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        context = getActivity();
        View view = inflater.inflate(R.layout.fragment_topic, container, false);
        layoutProgress = (RelativeLayout) view.findViewById(R.id.rl_progress);
        lv = (ListView) view.findViewById(R.id.lv);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                startActivity(new Intent(context, DetailActivity.class));
            }
        });
        ptrv = (PtrClassicFrameLayout) view.findViewById(R.id.ptrv);
        ptrv.setLastUpdateTimeHeaderRelateObject(context);
        ptrv.setMode(PtrFrameLayout.Mode.REFRESH);
        ptrv.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(final PtrFrameLayout frame) {
                ptrv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        frame.refreshComplete();
                    }
                }, 1000);
            }

            @Override
            public void onRefreshBegin(final PtrFrameLayout frame) {
                ptrv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        frame.refreshComplete();
                    }
                }, 1000);
            }
        });
        ptrv.setVisibility(View.VISIBLE);
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), getData(),
                R.layout.item_vip_card, new String[]{"img"},
                new int[]{R.id.iv_cover});
        lv.setAdapter(adapter);
        layoutProgress.setVisibility(View.GONE);
//        initListView();
//        initBanner();
        return view;
    }

    private void initBanner() {
        topView = LayoutInflater.from(context).inflate(R.layout.layout_topic_banner, null);
        rv = (RecyclerView) topView.findViewById(R.id.rv);
        rv.setFocusable(false);
        banner = (Banner) topView.findViewById(R.id.banner);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setDelayTime(3000);

        List<String> imgList = new ArrayList<>();
        imgList.add("http://www.ymwbw.cn/upload_files/article/31/201607/19_20160727100757_epnqz.jpg");
        imgList.add("http://139.129.108.163:84/uploadfile/2016/0808/20160808095924996.jpg");
        imgList.add("http://www.ymwbw.cn/upload_files/article/31/201607/19_20160727100757_epnqz.jpg");
        banner.setImages(imgList);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv.setLayoutManager(linearLayoutManager);
        ActiveAdapter activeAdapter = new ActiveAdapter();
        rv.setAdapter(activeAdapter);
        lv.addHeaderView(topView);
    }

    private void initListView() {
        TopicAdapter topicAdapter = new TopicAdapter();
        lv.setAdapter(topicAdapter);
    }

    class TopicAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 20;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = View.inflate(context, R.layout.item_topic, null);
                holder.tv_tab = (TextView) view.findViewById(R.id.tv_tab);
                holder.tv_title = (TextView) view.findViewById(R.id.tv_title);
                holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
                holder.tv_content = (TextView) view.findViewById(R.id.tv_content);
                holder.tv_like = (TextView) view.findViewById(R.id.tv_like);
                holder.civ_avtar = (ImageView) view.findViewById(R.id.civ_avtar);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            return view;
        }
    }

    class ViewHolder {
        private TextView tv_tab;
        private TextView tv_title;
        private TextView tv_name;
        private TextView tv_content;
        private TextView tv_like;
        private ImageView iv_cover;
        private ImageView civ_avtar;
    }

    class ActiveAdapter extends RecyclerView.Adapter<ViewHolderActive> {
        @Override
        public ViewHolderActive onCreateViewHolder(ViewGroup parent, int viewType) {
            View view =
                    LayoutInflater.from(parent.getContext()).inflate(R.layout.item_topic_hor_scroll, parent, false);
            ViewHolderActive holder = new ViewHolderActive(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(ViewHolderActive holder, int position) {
            if (position == 9) {
                holder.view_1.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public int getItemCount() {
            return 10;
        }
    }

    public static class ViewHolderActive extends RecyclerView.ViewHolder {
        ImageView iv_cover;
        TextView tv_name;
        View view_1;

        public ViewHolderActive(View view) {
            super(view);
            iv_cover = (ImageView) view.findViewById(R.id.iv_cover);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            view_1 = (View) view.findViewById(R.id.view_1);
        }
    }


    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("img", R.mipmap.vip_1);
//        map.put("title", "小宗");
//        map.put("info", "电台DJ");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.mipmap.vip_2);
//        map.put("title", "貂蝉");
//        map.put("info", "四大美女");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.mipmap.vip_3);
//        map.put("title", "奶茶");
//        map.put("info", "清纯妹妹");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.mipmap.vip_4);
//        map.put("title", "大黄");
//        map.put("info", "是小狗");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.mipmap.vip_new);
//        map.put("title", "hello");
//        map.put("info", "every thing");
        list.add(map);
        return list;
    }
}
